﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcTasks.Controllers;
using System.Web.Mvc;
using MvcTasks.Models;
using Moq;
using System.Collections.Generic;

namespace MvcTasks.Tests.Controllers
{
    [TestClass]
    public class TasksControllerTests
    {
        private TestDbSet<RandomTask> randomTasks = new TestDbSet<RandomTask>()
        {
            new RandomTask{ ID = 1, Title= "no1", Priority = 2},
            new RandomTask{ ID = 2, Title= "no2", Priority = 100},
        };

        public IDbContextWrapper wrapper;
        [TestMethod]
        public void Index()
        {
            var tasksCalled = 0;
            var OpenCalled = 0;
            var disposedCalled = 0;

            var dbContextWrapper = new Mock<IDbContextWrapper>();
            dbContextWrapper.Setup(x => x.RandomTask).Returns(new TestDbSet<RandomTask> { new RandomTask { Title = "Foo" } }).Callback(() => tasksCalled++);
            dbContextWrapper.Setup(x => x.Open()).Returns(dbContextWrapper.Object).Callback(() => OpenCalled++);
            dbContextWrapper.Setup(x => x.Dispose()).Callback(() => disposedCalled++);

            //Arrange
            TasksController controller = new TasksController(dbContextWrapper.Object);

             // Act
            ViewResult result = controller.Index() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(tasksCalled, 1);
            Assert.AreEqual(OpenCalled, 1);
            Assert.AreEqual(disposedCalled, 1);
        }

        [TestMethod]
        public void ItemDetailsFound()
        {
            var dbContextWrapper = new Mock<IDbContextWrapper>();
            var tasksCalled = 0;
            dbContextWrapper.Setup(x => x.RandomTask).Returns(new TestDbSet<RandomTask> { new RandomTask { Title = "Foo" , ID=1} }).Callback(() => tasksCalled++);

            TasksController controller = new TasksController(dbContextWrapper.Object);

            // Act
            ViewResult result = controller.ItemDetails(1) as ViewResult;
            Assert.IsNotNull(result);
            Assert.AreEqual(tasksCalled, 1);

        }

        [TestMethod]
        public void ItemDetailsNotFound()
        {
            var dbContextWrapper = new Mock<IDbContextWrapper>();
            var tasksCalled = 0;
            dbContextWrapper.Setup(x => x.RandomTask).Returns(new TestDbSet<RandomTask> { new RandomTask { Title = "Foo", ID = 1 } }).Callback(() => tasksCalled++);

            TasksController controller = new TasksController(dbContextWrapper.Object);

            // Act
            ViewResult result = controller.ItemDetails(12) as ViewResult;
            Assert.IsNull(result);
            Assert.AreEqual(tasksCalled, 1);
        }

        [TestMethod]
        public void GetMeTests()
        {
            var dbContextWrapper = new Mock<IDbContextWrapper>();
            var tasksCalled = 0;
            dbContextWrapper.Setup(x => x.RandomTask).Returns(randomTasks).Callback(() => tasksCalled++);


            TasksController controller = new TasksController(dbContextWrapper.Object);

            int count1 = 0;
            int count2 = 0;
            // Act
            for (int i = 0; i < 1000; i++)
            {
                ViewResult result = controller.GetMe() as ViewResult;
                Assert.IsNotNull(result);
                var id = ((RandomTask)(result.Model)).ID;
                if (id == 1) count1++;
                else if (id == 2) count2++;
                System.Threading.Thread.Sleep(50);
            }
            Console.WriteLine(count1 + " " + count2);
            Assert.IsTrue(count2 < count1);
           
        }
    }
}
